layui.use(['table', 'admin', 'ax', 'func', 'form','laydate'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var form = layui.form;
    var laydate = layui.laydate;

    /**
     * 管理
     */
    var BtyGufenzhuanrang = {
        tableId: "btyGufenzhuanrangTable"
    };

    var month = new Date().getMonth() + 1;
    //渲染时间选择框
    laydate.render({
        elem: '#month',
        format:'yyyy-MM',
        // value:month < 10 ? new Date().getFullYear() + "-0" + month : new Date().getFullYear() + "-" + month,//默认当月
        type: 'month',
        done:function(value,date,startDate){

        }

    });

    /**
     * 检索店名
     */
    var languageAjax = new $ax(Feng.ctxPath + "/btyGufenzhuanrang/getDm");
    var languageResult = languageAjax.start();
    $.each(languageResult, function( index, val ) {
        $("#dm").append("<option value='"+val.dm +"'>"+val.dm +"</option>");
    } );
    form.render('select');

    /**
     * 检索转让方
     */
    var languageAjax = new $ax(Feng.ctxPath + "/btyGufenzhuanrang/getZr");
    var languageResult = languageAjax.start();
    $.each(languageResult, function( index, val ) {
        $("#zr").append("<option value='"+val.zrTel +"'>"+val.zrName +"</option>");
    } );
    form.render('select');

    /**
     * 检索受让方
     */
    var languageAjax = new $ax(Feng.ctxPath + "/btyGufenzhuanrang/getSr");
    var languageResult = languageAjax.start();
    $.each(languageResult, function( index, val ) {
        $("#sr").append("<option value='"+val.srTel +"'>"+val.srName +"</option>");
    } );
    form.render('select');

    /**
     * 初始化表格的列
     */
    BtyGufenzhuanrang.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {align: 'center', type: "numbers", sort: false, title: '序号'},
            {field: 'btyGufenzhuanrangShepiId', hide: true},
            {field: 'dm', sort: false, title: '转让门店'},
            {
                field: 'zrBili', sort: false, title: '转让比例',align: "center", templet: function (d){
                    if (d.zrBili === undefined || d.zrBili === ""){
                        return d.zrBili;
                    }else {
                        return d.zrBili + "%";
                    }
                }
             },
            {field: 'month', sort: false, title: '分红日期'},
            {field: 'zrName', sort: false, title: '转让方(甲方)'},
            {field: 'zrDept', sort: false, title: '转让方部门'},
            {field: 'zrZhiwei', sort: false, title: '转让方职位'},
            {field: 'zrSfz', sort: false, title: '转让方身份证号码'},
            {field: 'zrTel', sort: false, title: '转让方电话'},
            {field: 'srName', sort: false, title: '受让方(乙方)'},
            {field: 'srDept', sort: false, title: '受让方部门'},
            {field: 'srZhiwei', sort: false, title: '受让方职位'},
            {field: 'srSfz', sort: false, title: '受让方身份证'},
            {field: 'srTel', sort: false, title: '受让方电话'},
            {
                field: 'xieyi', sort: false, title: '个人转让协议', templet: function (d) {
                    return "<img src='" + d.xieyi + "' style='height: 100%; max-width: 100%;' onclick='imgViewer(this)' >";
                }
            },
            {field: 'createTime', sort: false, title: '申请时间'},
            {field: 'shenpiShijian', sort: false, title: '审核时间'},
            {field: 'status', sort: false, title: '审核状态', fixed:'right',templet: function (d) {
                    if (d.status == '1') {
                        return "待审核";
                    } else if (d.status == '2') {
                        return "已驳回";
                    } else if (d.status == '3') {
                        return "已通过";
                    }
                }
            },
            {align: 'center', toolbar: '#tableBar', title: '操作', minWidth: 200,fixed:'right'}
        ]];
    };

    // 重置按钮事件
    $('#btnReset').click(function () {
        $('input').val('');
        $('#status').val('1');
        $("#dm").val('');
        $("#zr").val('');
        $("#sr").val('');
        // $("#month").val(month < 10 ? new Date().getFullYear() + "-0" + month : new Date().getFullYear() + "-" + month);
        $("#month").val('');
        form.render();
    });

    /**
     * 点击查询按钮
     */
    BtyGufenzhuanrang.search = function () {
        var queryData = {};
        queryData['status'] = $("#status").val();
        queryData['dm'] = $("#dm").val();
        queryData['zrTel'] = $("#zr").val();
        queryData['srTel'] = $("#sr").val();
        queryData['month'] = $("#month").val();
        table.reload(BtyGufenzhuanrang.tableId, {
            where: queryData, page: {curr: 1}
        });
    };
    form.on('select(startWorkYn)', function (data) {
        BtyGufenzhuanrang.search();
    });

    /**
     * 导出excel按钮
     */
    BtyGufenzhuanrang.exportExcel = function () {
        //获取导出的数据
        var ajax = new $ax(Feng.ctxPath + "/btyGufenzhuanrang/expList");
        var result = ajax.start();
        table.exportFile(tableResult.config.id, result, 'xls');
    };

    /**
     * 通过
     *
     * @param data 点击按钮时候的行数据
     */
    var _delay = false;
    BtyGufenzhuanrang.passOrRebut = function (data, status) {
        var operation = function () {
            if(_delay) {
                return;
            };
            _delay = true;
            setTimeout(() =>{
                _delay = false
            },2000);
            var ajax = new $ax(Feng.ctxPath + "/btyGufenzhuanrang/passOrRebut", function () {
                Feng.success("已通过!");
                table.reload(BtyGufenzhuanrang.tableId);
            }, function (data) {
                Feng.error("出现错误!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.btyGufenzhuanrangShepiId);
            ajax.set("fatherId", data.btyGufenzhuanrangId);
            ajax.set("status", status);
            ajax.set("zrTel", data.zrTel);
            ajax.set("jiedian", data.jiedian);
            ajax.set("shopCode", data.shopId);
            ajax.start();
        };
        Feng.confirm("是否通过?", operation);
    };

    /**
     * 点击驳回
     *
     * @param data 点击按钮时候的行数据
     */
    BtyGufenzhuanrang.rebut = function (data, status) {
        var index = layer.open({
            area: ['300px', '250px'],
            title: '审核',
            btn: ['确认'],
            btnAlign: 'c',
            type: 1,
            async: false,
            content: ' <div style="text-align:center;" id="div">\n' +
                ' <textarea type="text" class="input" id="reason" placeholder="请填写驳回理由" style="height: 100px;width: 92%;margin-top: 10px;padding-left: 5px;"></textarea>\n' +
                ' </div>',
            yes: function (index, layero) {
                var reason = $('#reason').val();
                if (reason == '') {
                    layer.msg('请填写驳回理由！', {icon: 2},);
                    return false;
                } else {
                    var ajax = new $ax(Feng.ctxPath + "/btyGufenzhuanrang/passOrRebut", function () {
                        Feng.success("已驳回!");
                        table.reload(BtyGufenzhuanrang.tableId);
                    }, function (data) {
                        Feng.error("出现错误!" + data.responseJSON.message + "!");
                    });
                    ajax.set("id", data.btyGufenzhuanrangShepiId);
                    ajax.set("fatherId", data.btyGufenzhuanrangId);
                    ajax.set("status", status);
                    ajax.set("reason", reason);
                    ajax.set("jiedian", data.jiedian);
                    ajax.set("shopCode", data.shopId);
                    ajax.set("zrTel", data.zrTel);
                    ajax.start();
                    layer.close(index);
                }
            },
            btn2: function (index, layero,) {
                layer.close(index);
            },
        });
        /*var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/btyGufenzhuanrang/passOrRebut", function () {
                Feng.success("已驳回!");
                table.reload(Notice.tableId);
            }, function (data) {
                Feng.error("出现错误!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.btyGufenzhuanrangShepiId);
            ajax.set("status", status);
            ajax.start();
        };*/
    };


    BtyGufenzhuanrang.shChain = function (data) {
        func.open({
            height: 690,
            width: "500px",
            title: '审核链',
            content: Feng.ctxPath + '/btyGufenzhuanrang/sh_chain?btyGufenzhuanrangId=' + data.btyGufenzhuanrangId,
            tableId: BtyGufenzhuanrang.tableId
        });
    };


    // 渲染表格
    var tableResult = table.render({
        elem: '#' + BtyGufenzhuanrang.tableId,
        url: Feng.ctxPath + '/btyGufenzhuanrang/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        where: {status: $('#status').val()
            // , month: month < 10 ? new Date().getFullYear() + "-0" + month : new Date().getFullYear() + "-" + month
        },
        cols: BtyGufenzhuanrang.initColumn(),
        done: function (res) {
            if (res.data == '' ){
                $('.layui-table-box').css('overflow','auto')
                $('.layui-table-header, .layui-table-body').css('overflow','visible')
            }
        }
    });

    imgViewer = function (img) {
        let _tr = $(img).parents('tr')
        let index = $(_tr).index()

        // let $image = $('.layui-table-main tbody tr[data-index= '+ index + '] img')

        $('.layui-table-main tbody tr[data-index= '+ index + ']').viewer()
        return
    };

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        BtyGufenzhuanrang.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {

        BtyGufenzhuanrang.jumpAddPage();

    });

    // 导出excel
    $('#btnExp').click(function () {
        BtyGufenzhuanrang.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + BtyGufenzhuanrang.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'pass') {
            BtyGufenzhuanrang.passOrRebut(data, 3);
        } else if (layEvent === 'rebut') {
            BtyGufenzhuanrang.rebut(data, 2);
        }else if (layEvent === 'shChain'){
            BtyGufenzhuanrang.shChain(data);
        }
    });
});
